﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProgNetDxComponents.ExtenderProviders.Settings;
using DevExpress.XtraEditors;
using System.ComponentModel;
using ProgNetDxComponents.ExtenderProviders.Options;

namespace ProgNetDxComponents.ExtenderProviders
{
    public class OptionProvider : ProgNetComponents.ExtenderProviders.BaseExtenderProvider<OptionSettings>
    {
        public OptionProvider(IContainer container)
            : base(container)
        {

        }
        public OptionProvider() { }

        protected override void ProcessObject(object sender, OptionSettings settings)
        {
            if (sender is BaseEdit)
            {
                if (this.IsAppRunning())
                {
                    BaseEdit edit = (sender as BaseEdit);
                    edit.EditValueChanging -= new DevExpress.XtraEditors.Controls.ChangingEventHandler(edit_EditValueChanging);
                    edit.EditValueChanging += new DevExpress.XtraEditors.Controls.ChangingEventHandler(edit_EditValueChanging);

                    if (settings.OptionName.IsNotNull())
                    {
                        edit.Properties.LockEvents();
                        edit.EditValue = OptionCache.GetOptionValue(settings.OptionName);
                        edit.Properties.UnLockEvents();
                    }
                }
            }
        }

        void edit_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            OptionSettings s = GetSettings(sender);
            if (s.OptionName.IsNotNull())
                OptionCache.SetOptionValue(s.OptionName, e.NewValue);
        }
        public override bool CanExtend(object extendee)
        {
            if (extendee is BaseEdit)
                return true;
            return false;
        }
    }
}
